package com.leesche.zcsp.node.web.controller.wechat.operate;

import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.utils.UtilDate;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.service.PubDevicePayDetailStatisticsService;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/11/3.
 * 页面信息搜索
 */
@Controller
@RequestMapping("/wechat/operate/search")
public class WechatSearchController {
    private final static Logger LOGGER = LoggerFactory.getLogger(WechatSearchController.class);

    private final static String SEARCHDEVICEVIEW = "wechat/operate/search/searchDeviceView";
    private final static String SEARCHACCOUNTVIEW = "wechat/operate/search/searchAccountView";
    private final static String DEVICESVIEW = "wechat/operate/search/deviceView";
    private final static String ACCOUNTSVIEW = "wechat/operate/search/accountView";

    @Autowired
    private PrivilegeService privilegeService;
    @Autowired
    private PubDeviceService deviceService;
    @Autowired
    private PubPlaceService placeService;
    @Autowired
    private NetService netService;
    @Autowired
    private PubDevicePayDetailStatisticsService detailStatisticsService;

    /**
     * 设备搜索界面
     * @return
     */
    @RequestMapping(value = "/searchDeviceView", method = RequestMethod.GET)
    public String searchDeviceView(
            Model model
    ){
        getView(model);
        return SEARCHDEVICEVIEW;
    }

    /**
     * 账目搜索界面
     * @return
     */
    @RequestMapping(value = "/searchAccountView", method = RequestMethod.GET)
    public String searchAccountView(
            Model model
    ){
        getView(model);
        return SEARCHACCOUNTVIEW;
    }

    /**
     * 场地查询设备
     * @param placeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryPlace2Device", method = RequestMethod.POST)
    public Map<String, Object> queryPlace2Device(
            @RequestParam("placeId")Long placeId
    ){
        try {
            Long adminId = getAdminId();

            QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
            condition.setAdminId(adminId);
            condition.setPlaceId(placeId);
            List<InfoDeviceDto> deviceList = deviceService.findDeviceList(condition);

            return WebResult.success().put("deviceList", deviceList).info("操作成功").toMap();
        } catch (Exception e) {
            LOGGER.info(e.getMessage(),e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 设备条件查询
     * @param placeId
     * @param deviceId
     * @param deviceShortCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDevices", method = RequestMethod.POST)
    public Map<String, Object> queryDevices(
            @RequestParam("placeId")Long placeId,
            @RequestParam("deviceId")Long deviceId,
            @RequestParam("deviceShortCode")String deviceShortCode
    ){
        Long adminId = getAdminId();
        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        condition.setAdminId(adminId);
        condition.setPlaceId(placeId);
        condition.setDeviceId(deviceId);
        condition.setDeviceshortcode(deviceShortCode);
        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(condition);

        List<Long> deviceIds = new ArrayList<Long>();
        for(InfoDeviceDto device : deviceList){
            deviceIds.add(device.getDeviceId());
        }

        return WebResult.success().put("placeId",placeId!=null?placeId:0).put("deviceIds",deviceIds).info("操作成功").toMap();
    }

    /**
     * 设备查询结果列表页面
     * @param deviceIds
     * @param model
     * @return
     */
    @RequestMapping(value = "/deviceView",method = RequestMethod.GET)
    public String deviceView(
            @RequestParam("placeId")Long placeId,
            @RequestParam("deviceIds")Long[] deviceIds,
            Model model
    ){
        Long adminId = getAdminId();

        PubPlaceDto placeDto = new PubPlaceDto();
        if(placeId!=0){
            QueryPubPlaceCondition placeCondition = new QueryPubPlaceCondition();
            placeCondition.setPlaceId(placeId);
            placeDto = placeService.findByCondition(placeCondition);
        }else{
            placeDto.setAddress("全场地全设备");
        }

        if(deviceIds.length>0){
            List<InfoDeviceDto> onlineList = new ArrayList<InfoDeviceDto>();
            List<InfoDeviceDto> offlineList = new ArrayList<InfoDeviceDto>();
            QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
            condition.setAdminId(adminId);
            for(Long deviceId : deviceIds){
                condition.setDeviceId(deviceId);
                InfoDeviceDto deviceDto = deviceService.findInfoDevice(condition);
                boolean flag = netService.isOnline(deviceDto.getImme());
                if (flag == true) {
                    onlineList.add(deviceDto);
                } else {
                    offlineList.add(deviceDto);
                }
            }
            model.addAttribute("onlineList",onlineList);
            model.addAttribute("offlineList",offlineList);
        }

        model.addAttribute("placeName",placeDto.getAddress());
        return DEVICESVIEW;
    }

    /**
     *
     * @param placeId
     * @param deviceId
     * @param stime
     * @param etime
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryAccounts", method = RequestMethod.POST)
    public Map<String, Object> queryAccounts(
            @RequestParam("placeId")Long placeId,
            @RequestParam("deviceId")Long deviceId,
            @RequestParam("stime")String stime,
            @RequestParam("etime")String etime
    ){
        Long adminId = getAdminId();
        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        condition.setAdminId(adminId);
        condition.setPlaceId(placeId);
        condition.setDeviceId(deviceId);
        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(condition);

        List<Long> deviceIds = new ArrayList<Long>();
        for(InfoDeviceDto device : deviceList){
            deviceIds.add(device.getDeviceId());
        }

        return WebResult.success().put("placeId",placeId!=null?placeId:0).put("deviceIds",deviceIds).put("stime",stime).put("etime",etime).info("操作成功").toMap();
    }


    /**
     * 账目查询结果页面
     * @param placeId
     * @param deviceIds
     * @param stime
     * @param etime
     * @param model
     * @return
     */
    @RequestMapping(value = "/accountView",method = RequestMethod.GET)
    public String accountView(
            @RequestParam("placeId")Long placeId,
            @RequestParam("deviceIds")Long[] deviceIds,
            @RequestParam("stime")String stime,
            @RequestParam("etime")String etime,
            Model model
    ){
        Long adminId = getAdminId();
        String today = UtilDate.getSimpleDate();
        String placeName;
        String deviceName="";

        QueryPubDevicePayDetailStatisticsCondition statisticsCondition = new QueryPubDevicePayDetailStatisticsCondition();
        statisticsCondition.setUserId(adminId);
        statisticsCondition.setStime(stime);
        statisticsCondition.setEtime(etime);
        PubDevicePayDetailStatisticsDto detailStatisticsDto;

        if(placeId==0 && deviceIds.length>0){
            detailStatisticsDto = detailStatisticsService.findByCondition(statisticsCondition);
            placeName = "全场地全设备";
        }else{
            if(deviceIds.length==1){
                Long deviceId = deviceIds[0];
                QueryPubDeviceCondition deviceCondition = new QueryPubDeviceCondition();
                deviceCondition.setDeviceId(deviceId);
                InfoDeviceDto deviceDto = deviceService.findInfoDevice(deviceCondition);
                statisticsCondition.setDeviceId(deviceId);
                detailStatisticsDto = detailStatisticsService.findByCondition(statisticsCondition);
                placeName = deviceDto.getAddress();
                deviceName = deviceDto.getDeviceName();
                model.addAttribute("deviceId",deviceId);
            }else{
                QueryPubPlaceCondition placeCondition = new QueryPubPlaceCondition();
                placeCondition.setPlaceId(placeId);
                PubPlaceDto placeDto = placeService.findByCondition(placeCondition);

                detailStatisticsDto = detailStatisticsService.findByCondition(statisticsCondition);
                placeName = placeDto.getAddress();
            }
        }
        model.addAttribute("placeName",placeName);
        model.addAttribute("deviceName",deviceName);
        model.addAttribute("detailStatisticsDto",detailStatisticsDto);
        model.addAttribute("today",today);
        model.addAttribute("stime",stime);
        model.addAttribute("etime",etime);
        return ACCOUNTSVIEW;
    }

    /**
     * 返回初始化场地和设备
     * @param model
     * @return
     */
    private Model getView(Model model){
        Long adminId = getAdminId();

        QueryPubPlaceCondition placeCondition = new QueryPubPlaceCondition();
        placeCondition.setAdminId(adminId);
        List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);

        QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
        condition.setAdminId(adminId);
        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(condition);

        model.addAttribute("placeList",placeList);
        model.addAttribute("deviceList",deviceList);
        return model;
    }

    private Long getAdminId(){
        Principal principal = privilegeService.getCurrentPrincipal();
        Long adminId;
        if(principal.getParentid()==1){
            adminId = principal.getUserid();
        }else{
            adminId = principal.getParentid();
        }
        return adminId;
    }

}
